package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.common.utils.TraceUtil;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.UserConstants;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.pojo.DictType;
import com.tbit.uqbike.object.pojo.TransactionLog;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.DictTypeService;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.RequestContext;

import java.util.List;

/**
 * 数据字典信息
 *
 * @author jing
 */
@RestController
@RequestMapping("/dictType")
public class DictTypeController extends BaseController {
    @Autowired
    private DictTypeService dictTypeService;

    @GetMapping("/list")
    public JsonResult list(DictType dictType) {
        startPage();
        List<DictType> list = dictTypeService.selectDictTypeList(dictType);
        return JsonResult.ok(list, (int) new PageInfo<>(list).getTotal());
    }


    /**
     * 查询字典类型详细
     */
    @GetMapping(value = "/{dictId}")
    public JsonResult getInfo(@PathVariable Integer dictId) {
        return JsonResult.ok(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @PostMapping
    @OperateLog(module = ModuleEnum.DICTTYPE, methods = MethodEnum.DICTTYPE_ADD)
    public JsonResult add(@Validated @RequestBody DictType dict) {
        RequestContext context = getLogin(ServletUtils.getRequest()).context;
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return JsonResult.fail(context.getMessage("insert_dict_type_exists"), TraceUtil.getTraceIdByLocal());
        }
        return JsonResult.toAjax(dictTypeService.insertDictType(dict), context);
    }

    /**
     * 修改字典类型
     */
    @PutMapping
    @OperateLog(module = ModuleEnum.DICTTYPE, methods = MethodEnum.DICTTYPE_EDIT)
    public JsonResult edit(@Validated @RequestBody DictType dict) {
        RequestContext context = getLogin(ServletUtils.getRequest()).context;
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return JsonResult.fail(context.getMessage("update_dict_type_exists"), TraceUtil.getTraceIdByLocal());
        }
        return JsonResult.toAjax(dictTypeService.updateDictType(dict), context);
    }

    /**
     * 删除字典类型
     */
    @DeleteMapping("/{dictIds}")
    @OperateLog(module = ModuleEnum.DICTTYPE, methods = MethodEnum.DICTTYPE_DEL)
    public JsonResult remove(@PathVariable Integer[] dictIds) {
        return JsonResult.toAjax(dictTypeService.deleteDictTypeByIds(dictIds), getLogin(ServletUtils.getRequest()).context);
    }

    /**
     * 清空缓存
     */
    @DeleteMapping("/clearCache")
    public JsonResult clearCache() {
        dictTypeService.clearCache();
        return JsonResult.ok();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public JsonResult optionselect() {
        List<DictType> dictTypes = dictTypeService.selectDictTypeAll();
        return JsonResult.ok(dictTypes);
    }
}
